package net.njcp.user;

import net.njcp.spi.SpiLoader;
import net.njcp.spi.exception.SpiProxyCompileException;
import org.junit.Test;

/**
 * @ClassName testUser
 * @Description: TODO
 * @Author 柳拓
 * @Date 2019/8/22
 * @Version V1.0
 **/
public class testUser {

    @Test
    public void testUserSPI() throws SpiProxyCompileException {
        //初始化实现类加载器
        SpiLoader<IUser> loader = SpiLoader.load(IUser.class);
        //生成一个代理类
        /**
         * package net.njcp.user;
         * import net.njcp.spi.SpiLoader;
         * public class IUser$Proxy implements net.njcp.user.IUser {
         *
         * public void welcome(net.njcp.user.UserDO arg0) {
         * try{
         * net.njcp.user.IUser spiImpl=SpiLoader.load(net.njcp.user.IUser.class).getService(arg0,"welcome");
         * spiImpl.welcome(arg0);
         * } catch(net.njcp.spi.exception.NoSpiMatchException e){
         * throw new java.lang.RuntimeException(e);
         * }
         * }
         * }
         */
        //net.njcp.user.IUser spiImpl=SpiLoader.load(net.njcp.user.IUser.class).getService(arg0,"welcome");
        //1、获取接口的所有属性
        //2、根觉配置的选择器选择对应的实现
        IUser user = loader.getAdaptive();


        UserDO weixinUser = new UserDO();
        weixinUser.setAvatar("weixin.avatar.jpg");
        weixinUser.setUname("微信用户");
        weixinUser.setMarket(MarketEnum.WEIXIN);
        //调用代理类的welcome方法
        user.welcome(weixinUser);


        UserDO qqUser = new UserDO();
        qqUser.setAvatar("qq.avatar.jpg");
        qqUser.setUname("qq用户");
        qqUser.setMarket(MarketEnum.QQ);
        user.welcome(qqUser);

        System.out.println("-----over------");
    }

}
